﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace POSPlus.BTxmlWriter
{
    public class XmlTree
    {
        public XmlDocument Document { get; private set; }

        public XmlTree()
        {
            Document = new XmlDocument();
        }

        public void AddNode(String name)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, name, null);
            Document.AppendChild(node);
        }

        public void AddNode(String path, String name)
        {
            XmlNode node = Document.CreateNode(XmlNodeType.Element, name, null);
            XmlNode parent = Document.SelectSingleNode(path);
            parent.AppendChild(node);
        }

        public void AddNode(XmlNode node)
        {
            Document.AppendChild(node);
        }

        public void AddNode(String path, XmlNode node)
        {
            XmlNode parent = Document.SelectSingleNode(path);
            parent.AppendChild(node);
        }

        public XmlAttribute CreateAttribute(String name)
        {
            return Document.CreateAttribute(name);
        }

        public XmlNode CreateNode(XmlNodeType type, String name)
        {
            return Document.CreateNode(type, name, null);
        }

        public XmlNode Find(String path)
        {
            return Document.SelectSingleNode(path);
        }

        public void AddAttribute(XmlNode node, XmlAttribute attribute)
        {
            node.Attributes.Append(attribute);
        }

        public void AddAttribute(String path, XmlAttribute attribute)
        {
            Find(path).Attributes.Append(attribute);
        }

        public void SetInnerText(String path, String value)
        {
            Find(path).InnerText = value;
        }

        public void SetAttributeValue(String path, String attribute, String value)
        {
            Find(path)[attribute].Value = value;
        }
    }
}
